<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/5/2
 * Time: 9:26
 */

namespace app\common\model;


use app\common\helper\Response;
use think\Db;

class Text
{
    protected $chatset=null;
    public function text($file){
        $_file=$file;
        $question_title='';
        $question_type='';
        $questions=[];
        $options=[];
        $preg_title='/^\d+[\.、]/u';
        $preg_type='/\[((多选题)|(矩阵题)|(输入框)|(文本))\]/u';
        $is_start=false;
        $file=fopen($file,'r');
        while(!feof($file)){
            $line=fgets($file);
            $line=$this->convert($line);
            $line=trim($line);
            if(preg_match($preg_title,$line)){
                preg_match($preg_type,$line,$match);
                if($is_start===true){
                    $questions[]=[
                        //去除类型显示
                        'name'=>$question_title,
                        'ext'=>$options,
                        'type'=>$question_type,
                        //fuck 支出个毛的5.4 这里5.4长度不能为0.凸(艹皿艹 )
                        'feng'=>count($options)?array_fill(0,count($options),0):[]
                    ];
                    $question_title='';
                    $options=[];
                    $question_type='';
                }else{
                    $is_start=true;
                }
                $question_title=preg_replace($preg_title,'',$line);
                $tmp=isset($match[1])?$match[1]:'单选题';
                switch($tmp){
                    case '多选题';
                        $question_type='checkbox';
                        break;
                    case '矩阵题':
                        $question_type='table';
                        break;
                    case '输入框':
                        $question_type='text';
                        break;
                    case '文本':
                        $question_type='textarea';
                        break;
                    default:
                        $question_type='radio';
                }
            }elseif(strlen($line)){
                if($question_type=='table'){
                    if(isset($options['th'])){
                        $options['td'][]=$line;
                    }else{
                        $ths=preg_split('/\W+/u',$line);
                        $level=count($ths);
                        $ths=array_map(function($t)use(&$level){
                            $t.=','.($level*0.5);
                            $level--;
                            return $t;
                        },$ths);
                        $options['th']=$ths;
                        $options['td']=[];
                    }
                }
                elseif (in_array($question_type,['text','textarea'])){
                    $options=[];
                }
                else{
                    $options[]=$line;
                }
            }
        }
        $questions[]=[
            'name'=>$question_title,
            'ext'=>$options,
            'type'=>$question_type,
            'feng'=>count($options)?array_fill(0,count($options),0):[]
        ];
        $qids=[];
        try{
            Db::startTrans();
            foreach ($questions as $question){
                if(strlen($question['name'])==0){
                    continue;
                }
                $question['name']=preg_replace($preg_type,'',$question['name']);
                $model=Question::create($question,true);
                $qids[]=$model->id;
            }
            $questionnaire=[
                'name'=>basename($_file,'.'.pathinfo($_file,PATHINFO_EXTENSION )),
                'qids'=>implode(',',$qids),
                'lock'=>'true',
                'count_people'=>0,
                'category'=>json_encode([
                    'name'=>'',
                    'children'=>[]
                ])
            ];
            Questionnaire::create($questionnaire);
            Db::commit();
        }catch (\Exception $e){
            return Response::error($e->getTraceAsString());
        }
        fclose($file);
        unlink($_file);
        return Response::success('',0,'插入成功');
    }
    public function convert($string){
        if(empty($this->chatset)){
            $this->chatset=mb_detect_encoding($string,['UTF-8','GBK','BIG-5'],true);
            if($this->chatset=='CP936')$this->chatset='GBK';
        }
        return mb_convert_encoding($string,'UTF-8',$this->chatset);
    }
}